<template>
	<view class="j-balance">
		<view class="j-b-card">
			<view class="j-b-card-title">提现金额</view>
			<view class="j-b-card-input">
				<text class="j-b-card-i-tag">￥</text>
				<input class="j-b-card-i-i" v-model="params.amount" focus type="number" placeholder="" clearable />
			</view>
			<view class="j-b-card-remarks">
				<u-input type="text" v-model="params.orderTitle" maxlength="50" clearable placeholder="备注(50字)" />
			</view>
		</view>
		<view class="j-b-btn">
			<u-button class="j-b-b" shape="circle" hover-class="none" @click="postWithdrawal">提现</u-button>
			<view class="j-b-btn-d">
				<text>当前余额{{walletInfo.totalBalance ? walletInfo.totalBalance : '0.00'}}元，最低提现{{minimum?minimum:'0.00'}}元，</text>
				<text>手续费{{charge}}，</text>
				<view class="j-b-btn-d-all" @click="withdrawalAll">全部提现</view>
			</view>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default{
		data() {
			return {
				params: {
					amount: '',
					orderTitle: ''
				},
				walletInfo:{},
				minimum:'',
				charge:''
			}
		},
		onNavigationBarButtonTap() {
			this.$u.api
				.postIsLogin()
				.then(res => {
					console.log(res);
					// 跳转到充值记录页面
					uni.navigateTo({
						url:'withdrawal-record'
					})
				})
				.catch(err => {
					console.log(err);
					// if (err.status == 401) {
					// 	// 跳转登录页面
					// 	uni.navigateTo({
					// 		url:'../../../login'
					// 	})
					// 	return false;
					// }
				});
		},
		onLoad() {
			// 余额查询
			this.postFindUserCapitalInfo();
			// 最低提现金额
			this.postMinimum()
			// 提现手续费
			this.postCharge()
		},
		onShow() {
			// 余额查询
			this.postFindUserCapitalInfo();
			//  最低提现金额
			this.postMinimum()
			// 提现手续费
			this.postCharge()
		},
		methods:{
			// 用户提现
			postWithdrawal() {
				// 判断金额是否为空
				if (this.$u.test.isEmpty(this.params.amount)) {
					this.$refs.uToast.show({
						title: '请输入充值金额'
					});
					return false;
				}
			
				// 判断金额格式是否正确
				if (!this.$u.test.amount(this.params.amount)) {
					this.$refs.uToast.show({
						title: '金额格式不正确'
					});
					return false;
				}
			
				this.$u.api
					.postWithdrawal(this.params)
					.then(res => {
						console.log(res);
						uni.navigateTo({
							url: './submit-examine?type=2&price=' + this.params.amount
						});
					})
					.catch(err => {
						console.log(err);
						// if (err.status == 401) {
						// 	uni.navigateTo({
						// 		url: '../../../login'
						// 	});
						// }
					});
			},
			// 我的钱包-查询用户资金统计信息
			postFindUserCapitalInfo() {
				this.$u.api
					.postFindUserCapitalInfo()
					.then(res => {
						console.log(res,'[提现页面-查询用户资金统计信息]');
						this.walletInfo = res.data;
					})
					.catch(err => {
						console.log(err);
						if (err.status == 401) {
							this.walletInfo = {};
						}
					});
			},
			// 查询最低提现金额
			postMinimum(){
				this.$u.api
					.postFindConfigByKey({
						configKey: 'WithdrawLowestAmount'
					})
					.then(res => {
						console.log(res,'[提现页面-查询最低提现金额]');
						this.minimum = res.data;
					})
					.catch(err => {
						console.log(err);
						if (err.status == 401) {
							this.minimum = '';
						}
					});
			},
			// 提现手续费
			postCharge(){
				this.$u.api
					.postFindConfigByKey({
						configKey: 'WithdrawPoundageRatio'
					})
					.then(res => {
						console.log(res,'[提现页面-查询最低提现金额]');
						this.charge = res.data *100 +'%';
					})
					.catch(err => {
						console.log(err);
						if (err.status == 401) {
							this.charge = '';
						}
					});
			},
			// 全部提现
			withdrawalAll(){
				this.params.amount = this.walletInfo.totalBalance;
			}
		}
	}
</script>

<style>
page {
	background-color: #f3f3f3;
}
</style>
<style scoped lang="scss">
.j-balance {
	padding: 25rpx;

	.j-b-card {
		padding: 40rpx 25rpx 0 25rpx;
		background: rgba(255, 255, 255, 1);
		border-radius: 14rpx;
		
		.j-b-card-title{
			color: #A3A3A3;
		}
		
		.j-b-card-input{
			position: relative;
			display: flex;
			align-items: center;
			
			.j-b-card-i-tag{
				font-size: 50rpx;
				font-weight: bold;
				position: absolute;
				left: 0;
				top: 28rpx;
			}
			
			.j-b-card-i-i{
				padding-left: 66rpx;
				padding-top: 18rpx;
				padding-bottom: 10px;
				font-size: 70rpx;
				// font-weight: bold;
				border-bottom: 1rpx solid #F1F1F1;
			}
		}
		.j-b-card-remarks{
			padding: 10rpx 0;
		}
	}
	
	.j-b-btn{
		margin-top: 50rpx;
		
		.j-b-b{
			color: #000000;
			background:linear-gradient(90deg,rgba(248,195,5,1),rgba(254,166,2,1));
			
			&:after{
				border: 0;
			}
		}
		
		.j-b-btn-d{
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 24rpx;
			color: #A3A3A3;
			margin-top: 20rpx;
			
			.j-b-btn-d-all{
				color: #E21616;
				
			}
		}
	}
}
</style>
